package leetcode_1901_2000;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.TreeMap;
import java.util.TreeSet;

public class LeeCode_1986_wrong {
    public static void main(String[] args) {
//        System.out.println(minSessions(new int[]{1, 2, 3}, 3));
        System.out.println(minSessions(new int[]{3,1,3,1,1}, 8));
    }
    private static int minSessions(int[] tasks, int sessionTime) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int task : tasks) {
            map.put(task, map.getOrDefault(task, 0) + 1);
        }
        int ans = 0;
        while (!map.isEmpty()){
            dfs(map, sessionTime);
            ans++;
        }
        return ans;
    }
    private static void dfs(TreeMap<Integer, Integer> map, int remain){
        Integer lower = map.lowerKey(remain + 1);
        if (lower == null){
            return;
        }
        if (map.get(lower) == 1){
            map.remove(lower);
        }else {
            map.put(lower, map.get(lower) - 1);
        }
        dfs(map, remain - lower);
    }
}